package com.zhuoyue.biz.core.api.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhuoyue.biz.core.api.dao.InfraredDeviceButtonDao;
import com.zhuoyue.biz.core.api.domain.InfraredDeviceButtonDO;
import com.zhuoyue.biz.core.api.service.InfraredDeviceButtonService;
import com.zhuoyue.common.constant.ResultCode;
import com.zhuoyue.common.dto.ResultDTO;

/**
 * @author wjming
 * @ClassName: InfraredDeviceButtonServiceImpl
 * @Description: 红外设备逻辑
 * @Date 2021-07-08 16:16
 */
@Service
public class InfraredDeviceButtonServiceImpl extends ServiceImpl<InfraredDeviceButtonDao, InfraredDeviceButtonDO> implements InfraredDeviceButtonService {

    @Autowired
    private InfraredDeviceButtonDao infraredDeviceButtonDao;

    private static final Integer CODE_LIST_200 = 200;

    @Override
    public InfraredDeviceButtonDO addInfraredDeviceButton(InfraredDeviceButtonDO button, String deviceId) {
        //查询所有已存在的编号(红外学习仪，若不是红外学习则省略此步骤)
        List<Integer> codeList = infraredDeviceButtonDao.selectAllButtonCodeByDeviceId(deviceId);

        int code = 0;
        if (CollectionUtils.isEmpty(codeList)) {
            code = 1;
        } else {
            if (codeList.size() == CODE_LIST_200) {
                throw new ZyServerException(ErrorType.INVALID_PARAMS.getCode(), "红外学习仪最多支持200个按钮学习！");
            } else {
                Map<Integer, Integer> map = new HashMap<>(5);
                for (Integer c : codeList) {
                    if (null != c) {
                        map.put(c, c);
                    }
                }
                for (int i = 1; i <= CODE_LIST_200; i++) {
                    if (null == map.get(i)) {
                        code = i;
                        break;
                    }
                }
            }
        }

        if (code < 1 || code > CODE_LIST_200) {
            throw new ZyServerException(ErrorType.INVALID_PARAMS.getCode(), "红外按钮编号错误！");
        }

        button.setCode(code);

        int count = infraredDeviceButtonDao.insert(button);
        if (count < 1) {
            throw new ZyServerException(ErrorType.Service_Fail.getCode(), "红外设备按钮新增失败！");
        }

        InfraredDeviceButtonDO buttonRes = infraredDeviceButtonDao.selectInfraredDeviceButtonById(button.getId());
        if (null == buttonRes) {
            throw new ZyServerException(ErrorType.INVALID_PARAMS.getCode(), "红外设备按钮查询失败！");
        }
        return buttonRes;
    }

    /**
     * 获取红外设备按钮信息
     *
     * @param query
     * @return
     */
    @Override
    public List<InfraredDeviceButtonDO> getButtonByQuery(InfraredDeviceButtonDO query) {

        List<InfraredDeviceButtonDO> buttonList = list(Wrappers.<InfraredDeviceButtonDO>lambdaQuery()
                .eq(InfraredDeviceButtonDO::getInfraredDeviceId, query.getInfraredDeviceId())
                .eq(InfraredDeviceButtonDO::getOrderNumber, query.getOrderNumber()));
        if (CollectionUtils.isEmpty(buttonList)) {
            Collections.emptyList();
        }
        return buttonList;
    }
}
